package com.buba.system.dao.impl;

import com.buba.system.dao.MenuDao;
import com.buba.system.pojo.Menu;
import com.buba.system.pojo.RoleRight;
import com.buba.util.DBPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class MenuDaoImpl implements MenuDao {
    QueryRunner queryRunner = new QueryRunner(DBPool.getDS());

    @Override
    public List<Menu> selMenu(int roleId) {
        String sql = "select * from roleinfo r,roleright rr,sysfun s where r.RoleId = rr.RoleId and s.NodeId = rr.NodeId and r.RoleId="+roleId;
        try {
            List<Menu> query = queryRunner.query(sql, new BeanListHandler<Menu>(Menu.class));
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Menu> selMenu1() {
        String sql = "select * from sysfun";
        try {
            List<Menu> query = queryRunner.query(sql, new BeanListHandler<Menu>(Menu.class));
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<RoleRight> selRoleRight(String roleId) {
        String sql = "select * from roleright where roleId ="+roleId;
        try {
            return queryRunner.query(sql,new BeanListHandler<RoleRight>(RoleRight.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void addRoleRight(Object[] o, String roleId) {
        String delsql = "delete from roleright where roleId ="+roleId;
        String addsql = "insert into roleright values(null,"+roleId+",?)";
        try {
            queryRunner.update(delsql);
            for (int i = 0; i < o.length; i++) {
                queryRunner.update(addsql,o[i]);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
